<?php
include_once $REP_CLASS . 'Selector.php';
include_once $REP_CLASS . 'Contact.php';

class Contactasso {

private $refcontact  = 0;
private $refassociation  = 0;
private $assocPDO;
private $sqlInsert;
private $reqInsert;
private $sqlDelete;
private $reqDelete;
private $sqlUpdate;
private $reqUpdate;

/**
 * Constructeur
 * @db : la connexion de la base de donn?e
 */
 function __construct($db){

      // Cr?ation de la connexion
      $this->assocPDO = $db;
      // Initialisation des requ?tes
      $this->initRequete();
      // Pr?paration des requ?tes
      $this->prepare();

}
/**
 * Initialisation des requ?tes pr?par?es
 */
private function initRequete(){

      $this->sqlInsert = "INSERT INTO contactasso (refcontact, refassociation) VALUES(?, ?)";
      $this->sqlDelete = "DELETE FROM contactasso WHERE `refcontact` = ? AND `refassociation` = ?";
      $this->sqlUpdate = "UPDATE contactasso SET refcontact=?, refassociation=? WHERE `refcontact` = ? AND `refassociation` = ?";

}
/**
 * Pr?paration des requ?tes
 */
private function prepare(){

      $this->reqInsert       = $this->assocPDO->prepare($this->sqlInsert);
      $this->reqDelete       = $this->assocPDO->prepare($this->sqlDelete);
      $this->reqUpdate       = $this->assocPDO->prepare($this->sqlUpdate);

}
/**
 * Insertion en base des infos de l'utilisateur
 */
public function insertion(){

      // Requete d'insertion
      $champs = array($this->refcontact, $this->refassociation);

      try
      {
         return $this->reqInsert->execute($champs);
      }
      catch(Exception $e)
      {
         echo 'Erreur : <br>'; print_r($this->reqInsert->errorInfo());
         throw new Exception('Une erreur est survenue lors de l\'insertion dans contactasso.' . $e->getMessage());
      }

}
/**
 * Modification des infos de l'utilisateur
 */
public function modification(){

      // Requete de modification
      $champs = array($this->refcontact, $this->refassociation);

      try
      {
         return $this->reqUpdate->execute($champs);
      }
      catch(Exception $e)
      {
         echo 'Erreur : <br>'; print_r($this->reqUpdate->errorInfo());
         throw new Exception('Une erreur est survenue lors de la modification dans contactasso.');
      }

}
/**
 * Suppression de l'utilisateur ? partir de son ID
 */
public function suppression(){

      // Requete de suppression
      try
      {
         return $this->reqDelete->execute(array($this->refcontact, $this->refassociation));
      }
      catch(Exception $e)
      {
         echo 'Erreur : <br>'; print_r($this->reqDelete->errorInfo());
         throw new Exception('Une erreur est survenue lors de la suppression dans contactasso.');
      }

}
/**
 * Select
 */
public function select(){

      $select = new Selector();
      $champs = array("refcontact"=>$this->refcontact, "refassociation"=>$this->refassociation);

      foreach($champs as $cle=>$valeur)
      {
        // Les Champs
        $select->addChamp($cle);
        // Les Crit?res
        if( !empty($valeur) )
         $select->addCritere($cle, $valeur);
      }

      // La Table
      $select->addFrom('contactasso');

      //echo 'requete : ' . $select->toString();
      return $this->assocPDO->query($select->toString());

}
/**
 * Charge les infos de contactasso
 */
public function charge(){

      $user = new Contactasso($this->assocPDO);
      $user->setRefcontact($this->getRefcontact());
      $user->setRefassociation($this->getRefassociation());
      // Lecture de la premi?re ligne
      $stmt = $user->select();
      $result = $stmt->fetch();

      $this->refcontact=$result['refcontact'];
$this->refassociation=$result['refassociation'];


      // Fermeture du curseur
      $stmt->closeCursor();

}
/**
 * Affiche les infos de l'utilisateur
 */
public function affichage(){

      $champs = array("refcontact"=>$this->refcontact, "refassociation"=>$this->refassociation);
      echo "==== Fiche Contactasso ==== <br>";
      foreach($champs as $cle=>$valeur)
      {
        echo $cle . " : " . $valeur;
        echo "<br>";
      }
      echo "==========================<br>";

}

/**
 * Select par utilisateur (trouve les associations créees par l'utilisateur)
 * Utilise pour les petites annonces
 */
public function selectContactAsso(){

      try
      {
      	 $req= "SELECT contactasso.refcontact as refcontact,concat(contact.nom,' ',contact.prenom ) as libelle ";
         $req=$req." FROM contactasso, contact where contactasso.refassociation=".$this->getRefassociation()." and ";
         $req=$req." contactasso.refcontact=contact.refcontact";

      	 return $this->assocPDO->query($req);
      }
      catch(Exception $e)
      {
         echo 'Erreur : <br>'.$req."**";
         throw new Exception('Une erreur est survenue lors du select des contacts de l\'association association.');
      }
}


/**
 * Retrieves the value from the field refcontact
 * @return String with the value of the field
 */
public function getRefcontact(){
  return $this->refcontact;
}
/**
 * Retrieves the value from the field refassociation
 * @return String with the value of the field
 */
public function getRefassociation(){
  return $this->refassociation;
}
/**
 * Set the value from the field refcontact
 * @param refcontact String with the value for the field
 */
public function setRefcontact($refcontact){
  $this->refcontact = $refcontact;
}
/**
 * Set the value from the field refassociation
 * @param refassociation String with the value for the field
 */
public function setRefassociation($refassociation){
  $this->refassociation = $refassociation;
}
}
?>